import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/commodity/categories/page/plusCommodityMode/page/plusCommodityByFormField/page/tasteSetting/page/plusTaste/provider/taste_provider.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'taste_label.dart';

class TasteCard extends StatelessWidget {
  final List<String> _recommendTastes = ['甜度', '辣度', '温度', '忌口'];

  final List<List<String>> _labels = [
    ['无糖', '少糖', '半糖', '多糖'],
    ['不辣', '微辣', '中辣', '重辣'],
    ['热饮', '常温', '去冰', '少冰'],
    ['不要葱', '不要蒜', '不要香菜', '不要辣'],
  ];

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: SingleChildScrollView(
        padding: EdgeInsets.symmetric(horizontal: 30.w),
        child: Consumer<TasteProvider>(
          builder: (context, tasteProvider, _) {
            return Column(
              children: <Widget>[
                BaseTextFormField(
                  maxLength: 20,
                  prefixText: '口味标签',
                  isRequired: true,
                  hintText: '例如：辣度、甜度',
                  initialValue: tasteProvider.taste.tasteName,
                  validator: (value) {
                    if (value == '') {
                      return '口味标签不能为空';
                    }

                    return null;
                  },
                  onChanged: (value) {
                    tasteProvider.taste.tasteName = value;
                  },
                  actions: _recommendTastes,
                  onActionsTap: (index) {
                    tasteProvider.clearAndAddLabels(_labels[index]);
                  },
                ),
                ListView.builder(
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  padding: EdgeInsets.all(0),
                  itemBuilder: (context, lableIndex) {
                    var current = tasteProvider.taste.labels[lableIndex];

                    return TasteLabel(
                      key: current.key,
                      initValue: current.label,
                      onRemove: () {
                        var tasteProvider = Provider.of<TasteProvider>(
                          context,
                          listen: false,
                        );
                        tasteProvider.removeLabel(lableIndex);
                      },
                      onChanged: (value) {
                        current.label = value;
                      },
                    );
                  },
                  itemCount: tasteProvider.taste.labels.length,
                ),
                Container(
                  padding: EdgeInsets.only(
                    top: ScreenUtil.instance.setWidth(46.5),
                    bottom: ScreenUtil.instance.setWidth(31.5),
                  ),
                  alignment: Alignment.centerRight,
                  child: LabelButton(
                    title: '+新增口味',
                    textStyle: TextStyleConstant().blue_28,
                    onTap: () {
                      tasteProvider.addLabel();
                    },
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
  }
}
